//
//  SaleTableViewController.m
//  Car
//
//  Created by lanou on 15/9/18.
//  Copyright (c) 2015年 朱鸿儒. All rights reserved.
//

#import "SaleTableViewController.h"
#import "SaleTableViewCell.h"
#import "SaleModel.h"
#import "HeaderSaleView.h"
#import "CarDetailViewController.h"
@interface SaleTableViewController ()
@property (nonatomic ,strong)NSMutableArray *saleArray;

@property (nonatomic ,strong)NSString *name;
@end

@implementation SaleTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorStyle = 0;
    [self.tableView registerClass:[SaleTableViewCell class] forCellReuseIdentifier:@"salecell"];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(show:) name:@"id" object:nil];
    
    
}
-(void)show:(NSNotification *)no{
    self.idString = no.userInfo[@"key"];
    [self reloadAndJosn];
}

#pragma  mark--reloadAndJosn
-(void)reloadAndJosn{
    self.saleArray = nil;
    NSString *str = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.8.8/cars/seriesprice-pm1-b%@-t2.json" ,self.idString];
    [LORequestManger GET:str success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
       NSDictionary *Dic = [dic[@"result"][@"fctlist"] firstObject];
        for (NSDictionary *dictionary in Dic[@"serieslist"]) {
            SaleModel *model = [SaleModel JsonWithDictionary:dictionary];
            self.name = Dic[@"name"];
            [self.saleArray addObject:model];
        }

        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (NSMutableArray *)saleArray{
    if (!_saleArray) {
        _saleArray = [[NSMutableArray alloc]init];
    }
    return _saleArray;
}
- (void)viewDidAppear:(BOOL)animated{
	[super viewDidAppear:YES];
    for (UIView *views in self.navigationController.navigationBar.subviews) {
        if ([views isKindOfClass:[UISegmentedControl class]]) {
            [views setHidden:NO];
        }
    }
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.saleArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SaleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"salecell" forIndexPath:indexPath];
    [cell setWithModel:self.saleArray[indexPath.row]];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.name;
}

#pragma mark--cell点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CarDetailViewController *carVC = [[CarDetailViewController alloc]init];
    carVC.url = [self.saleArray[indexPath.row] ID];
    [self.navigationController pushViewController:carVC animated:YES];
}

@end
